<template>
  <div class="publish-wrap">
    <el-form class="form">
      <el-form-item label-width="100px" label="信息发布：">
        <el-input :model="publish" type="text"/>
      </el-form-item>
      <el-form-item label-width="100px" label="发布人：">
        <el-input :model="publishId" type="text"/>
      </el-form-item>
      <el-form-item label-width="120px" label="发布人手机：">
        <el-input :model="phone" type="text"/>
      </el-form-item>
      <el-form-item label-width="100px" label="发布位置：">
        <el-input :model="address" type="text"/>
      </el-form-item>
      <el-form-item label-width="100px" label="状态：">
        <el-select v-model="statesValue" placeholder="请选择">
          <el-option
            v-for="item in states"
            :key="item.value"
            :label="item.label"
            :value="item.label"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="100px" label="更新时间：">
        <el-date-picker
          v-model="pickDate"
          :picker-options="pickerOptions2"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="left"/>
      </el-form-item>
      <el-form-item label-width="50px">
        <el-button type="primary" icon="el-icon-search">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border @cell-click="handleCellClick">
      <el-table-column prop="index" label="序号" width="50"/>
      <el-table-column prop="companyname" label="信息名称/企业名称" width="200"/>
      <el-table-column prop="detailedaddress" label="发布位置" width="200"/>
      <el-table-column prop="equtypename" label="类型" width="200"/>
      <el-table-column prop="linkname" label="发布人" width="200"/>
      <el-table-column prop="phone" label="发布人手机" width="200"/>
      <el-table-column prop="senddate" sortable label="更新时间" width="200"/>
      <el-table-column prop="senddate" sortable label="发布时间" width="200"/>
      <el-table-column prop="duedate" sortable label="到期时间" width="200"/>
      <el-table-column prop="payNum" label="缴费次数" width="100">
        <template slot-scope="scope">
          <div style="cursor: pointer;width: 100%;height: 50px;line-height: 50px" @click.stop="handleOrder(scope)">{{ scope.row.payNum }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="equimgurl" label="上传图片" min-width="300">
        <template slot-scope="scope">
          <div class="image-wrap">
            <img :src="`${baseUrl}/common/viewImage?imagepath=${scope.row.equimgurl}`" :preview="scope.$index" preview-text="" alt="" class="image">
          </div>
        </template>
      </el-table-column> -->
      <!-- <el-table-column prop="star" label="星级" width="200">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.star"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"/>
        </template>
      </el-table-column> -->
      <el-table-column prop="clickcount" sortable label="浏览次数" width="300">
        <template slot-scope="scope">
          <template v-if="scope.row.edit">
            <el-row class="edit-box">
              <el-input v-model="scope.row.clickCount" style="margin-right:10px" class="edit-input" size="small"/>
              <el-button class="cancel-btn" size="small" icon="el-icon-refresh" type="warning" @click.stop="cancelEdit(scope.row)">取消</el-button>
            </el-row>
          </template>
          <span v-else>{{ scope.row.clickcount }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="前端状态" width="220">
        <template slot-scope="scope">
          <el-tag :type="scope.row.state === 2 ? 'success' : 'danger'">{{ scope.row.state === 2 ? '正常': '禁用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="后台禁用" width="220">
        <template slot-scope="scope">
          <el-tag :type="scope.row.state === 2 ? 'success' : 'danger'">{{ scope.row.state === 2 ? '正常': '禁用' }}</el-tag>
          <!-- <el-switch
            v-model="scope.row.after"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="正常"
            inactive-text="禁用"
            class="switch-btn"
            @change="handleChange(scope)"/> -->
        </template>
      </el-table-column>
      <el-table-column prop="switch" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="primary" class="aduit-button" @click.stop="handleDelete(scope)">{{ scope.row.state === 1 ? '审核' : scope.row.state === 2 ? '审核通过' : '未通过' }}</el-button>
          <el-button v-if="scope.row.edit" type="success" icon="el-icon-circle-check-outline" @click.stop="confirmEdit(scope.row)">确认</el-button>
          <el-button v-else type="primary" icon="el-icon-edit" @click.stop="scope.row.edit=!scope.row.edit">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>10" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog
      :visible.sync="centerDialogVisible"
      title="审核"
      width="30%"
      center>
      <el-row>
        <el-radio v-model="radio" label="2">通过</el-radio>
        <el-radio v-model="radio" label="3">未通过</el-radio>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAttestation">确 定</el-button>
      </span>
    </el-dialog>
    <router-view/>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import base from '@/api/base'
import { rentMessage, rentAudit } from '@/api/message'
export default {
  name: 'Publish',
  components: {
    Pagination
  },
  data() {
    return {
      baseUrl: base.pro,
      publish: '',
      publishId: '',
      phone: '',
      address: '',
      statesValue: '',
      radio: '2',
      equtype: '',
      state: '',
      scope: {},
      states: [
        {
          value: '选项一',
          label: '未发布'
        },
        {
          value: '选项二',
          label: '正常'
        },
        {
          value: '选项三',
          label: '已停用'
        }
      ],
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      pickDate: '',
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 10
      },
      centerDialogVisible: false,
      iDisplayStart: 0,
      iDisplayLength: 10
    }
  },
  computed: {
    tableData() {
      const result = []
      this.list.map((item, index) => {
        const flag = item.state === 2
        item = Object.assign({}, item, { index: `${(this.listQuery.page - 1) * this.listQuery.limit + 1 + index}`, edit: false, after: flag })
        result.push(item)
      })
      return result
    }
  },
  created() {
    this.getRentMessage()
  },
  methods: {
    handleDelete(scope) {
      this.centerDialogVisible = true
      this.scope = scope
    },
    getList(prop) {
      this.listQuery.page = prop.page
      this.listQuery.limit = prop.limit
      this.iDisplayStart = (prop.page - 1) * prop.limit
      this.getRentMessage()
    },
    handleOrder() {
      this.$router.push({
        path: '/statistics/order'
      })
    },
    handleCellClick(row, column, cell, event) {
      const leading = 'leading'
      const after = 'after'
      const clickcount = 'clickcount'
      const equimgurl = 'equimgurl'
      console.log(row.id)
      // const id = 3
      if (column.property === leading || column.property === after || column.property === clickcount || column.property === equimgurl) return
      this.$router.push({
        path: '/message/rent/detail',
        query: {
          id: row.id
        }
      })
    },
    cancelEdit(params) {
      params.edit = false
    },
    confirmEdit(params) {
      params.edit = false
    },
    handleAttestation() {
      const { radio, scope } = this
      rentAudit(Number(radio), scope.row.id)
        .then((res) => {
          console.log(res)
          this.$message.success({
            message: '审核成功',
            duration: 1000
          })
          this.getRentMessage()
        }).catch((resw) => {
          this.$message.error({
            message: '请稍后再试',
            duration: 1000
          })
        })
    },
    hanleStop(e) {
      console.log(22)
    },
    getRentMessage() {
      this.list = []
      const { equtype, state } = this
      const iDisplayStart = this.iDisplayStart
      const iDisplayLength = this.iDisplayLength
      rentMessage(equtype, iDisplayStart, iDisplayLength, state)
        .then((res) => {
          console.log(res)
          this.total = res.total
          this.list = res.data
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.publish-wrap {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  .form {
    display: flex;
    flex-wrap: wrap;
  }
  .switch-btn {
    margin-top: 20px;
    margin-bottom: 20px;
    margin-left: 20px;
  }
  .edit-box{
    display: flex;
  }
  .aduit-button {
    margin-bottom: 10px;
  }
  [class = "image"] {
    width: 50px;
    height: 50px;
  }
}
</style>
